创建数组
numpy.empty
用于创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组;
数组元素为随机值,因为数组元素未初始化
语法:
numpy.empty(shape, dtype = float, order = 'C')
| 参数 | 说明 |
|---|---|
| shape | 数组形状 |
| dtype | 数据类型,可选 |
| order | 可选,'C'和'F',分别代表行优先和列优先,在计算机内存中存储元素的顺序 |
np_empty = np.empty((3,3), dtype = np.int)
print(np_empty)
# 输出结果:
# [[140097164208120 94907777961152 481036337261]
# [ 519691042932 261993005088 472446402592]
# [ 112 0 80]]
numpy.zeros
创建指定大小的数组,数组元素以 0 填充
语法:
numpy.zeros(shape, dtype = float, order = 'C')
| 参数 | 说明 |
|---|---|
| shape | 数组形状 |
| dtype | 数据类型,可选 |
| order | 可选,'C'和'F',分别代表行优先和列优先,在计算机内存中存储元素的顺序 |
# 默认为浮点数
zr_df = np.zeros(5)
print('默认类型:\n{}'.format(zr_df))
# 输出结果:
# 默认类型:
# [0. 0. 0. 0. 0.]
# 设置类型为整数
zr_int = np.zeros((5,),dtype = np.int)
print('设置为 int 型:\n{}'.format(zr_int))
# 输出结果:
# 设置为 int 型:
# [0 0 0 0 0]
# 自定义类型
zr_sf = np.zeros((2,2),dtype = [('x','i4'),('y','f4')])
print('自定义类型:\n{}'.format(zr_sf))
# 输出结果:
# 自定义类型:
# [[(0, 0.) (0, 0.)]
# [(0, 0.) (0, 0.)]]
numpy.ones
创建指定大小的数组,数组元素以 1 填充
语法:
numpy.ones(shape, dtype = float, order = 'C')
| 参数 | 说明 |
|---|---|
| shape | 数组形状 |
| dtype | 数据类型,可选 |
| order | 可选,'C'和'F',分别代表行优先和列优先,在计算机内存中存储元素的顺序 |
# 默认为浮点数
one_df = np.ones(5)
print('默认为 float 类型:\n{}'.format(one_df))
# 输出结果:
# 默认为 float 类型:
# [1. 1. 1. 1. 1.]
# 自定义为 int 类型
one_int = np.ones((5,), dtype = np.int)
print('自定义为 int类型:\n{}'.format(one_int))
# 输出结果:
# 自定义为 int类型:
# [1 1 1 1 1]
# 自定义为 complex 类型
one_cp = np.ones([2,2],dtype = complex)
print('自定义为 complex 类型:\n{}'.format(one_cp))
# 输出结果:
# 自定义为 complex 类型:
# [[1.+0.j 1.+0.j]
# [1.+0.j 1.+0.j]]
numpy.asarray
用于从已有的数组创建数组
语法:
numpy.asarray(a, dtype = None, order = 'C')
| 参数 | 说明 |
|---|---|
| a | 任意形式的输入参数,可以是列表、列表的元祖、元祖、元祖的元祖、元祖的列表和多维数组 |
| dtype | 数据类型,可选 |
| order | 可选,'C'和'F',分别代表行优先和列优先,在计算机内存中存储元素的顺序 |
num_list = [1, 2, 3]
# 列表转换为 ndarray
list_to_nd = np.asarray(num_list)
print(list_to_nd) # 输出结果为: [1 2 3]
num_tuple = (4, 5, 6)
# 元组转换为 ndarray
tuple_to_nd = np.asarray(num_tuple)
print(tuple_to_nd) # 输出结果为:[4 5 6]
list_tuple = [(1, 2, 3),(4, 5)]
# 元组列表转换为 ndarray
list_tuple_to_nd = np.asarray(list_tuple)
print(list_tuple_to_nd) # 输出结果为:[(1, 2, 3) (4, 5)]
num_list = [1, 2, 3]
# 设置 dtype参数为 int
nd_int = np.asarray(num_list, dtype = np.int)
print('设置 dtype 参数为 int:\n{}'.format(nd_int))
# 设置 dtype 参数为 float
nd_ft = np.asarray(num_list, dtype = np.float)
print('设置 dtype 参数为 float:\n{}'.format(nd_ft))
# 输出结果:
# 设置 dtype 参数为 int:
# [1 2 3]
# 设置 dtype 参数为 float:
# [1. 2. 3.]
numpy.frombuffer
用于实现动态数组,接收 buffer输入参数,以流的形式读入转化为 ndarray 对象
语法:
numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)
| 参数 | 说明 |
|---|---|
| buffer | 可以是任意对象,会以流的形式读入 |
| dtype | 返回数组的数据类型,可选 |
| count | 读取的数据数量,默认为-1,读取所有数据 |
| offset | 读取的起始位置,默认为 0 |
pri_str = b'Hello World'
fd_np = np.frombuffer(pri_str,dtype = 'S1')
print(fd_np)
# 输出结果:
# [b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']
numpy.fromiter
从可迭代对象中建立 ndarray对象,返回一维数组
语法:
numpy.fromiter(iterable, dtype, count = -1)
| 参数 | 说明 |
|---|---|
| iterable | 可迭代对象 |
| dtype | 返回数组的数据类型 |
| count | 读取的数据数量,默认为-1,读取所有数据 |
num_list = range(5)
iter_obj = iter(num_list)
np_int = np.fromiter(iter_obj, dtype = int)
print(np_int)
# 输出结果:
# [0 1 2 3 4]
numpy.arange
从指定范围创建数组,并返回 ndarray对象;
根据 start 和 stop 指定的范围以及 step设定的步长,生成一个 ndarray.
语法:
numpy.arange(start, stop, step, dtype)
| 参数 | 说明 |
|---|---|
| start | 起始值,默认为 0 |
| stop | 终止值(不包含) |
| step | 步长,默认为 1 |
| dtype | 返回 ndarray 的数据类型,如果没有提供,则会使用输入数据的类型 |
# 生成指定长度的数组
ar_np = np.arange(5)
print(ar_np) # 输出结果: [0 1 2 3 4]
# 生成指定长度的数组,并指定 dtype 为 float 类型
ft_ar = np.arange(5, dtype = float)
print('float 结果数组:{}'.format(ft_ar))
# 输出结果:
# float 结果数组:[0. 1. 2. 3. 4.]
# 生成指定长度的数组,并指定 dtype 为 complex 类型
cp_ar = np.arange(5, dtype=complex)
print('complex 结果数组:{}'.format(cp_ar))
# 输出结果:
# complex 结果数组:[0.+0.j 1.+0.j 2.+0.j 3.+0.j 4.+0.j]
# 设置步长为 2,终止值不包括在内
step_ar = np.arange(10, 20, 2)
print(step_ar) # 输出结果:[10 12 14 16 18]
numpy.linspace
创建一个一维数组,该数组由一个等差数列组成
语法:
numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)
| 参数 | 说明 |
|---|---|
| start | 序列的起始值 |
| stop | 序列的终止值,如果 endpoint为 True,该值包含在数列中 |
| num | 要生成的等步长的样本数量,默认为 50 |
| endpoint | 该值为 True时,数列中包含 stop 值,反之不包含,默认是 True |
| retstep | 如果为 True,则生成的数组中会显示间距,反之不显示,默认是 False |
| dtype | ndarray 的数据类型 |
# 默认为浮点数
ls_np_df = np.linspace(1,10,10)
print(ls_np_df) # 输出结果: [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
# 设置 dtype 为 int
ls_np_int = np.linspace(1,10,10,dtype = int)
print(ls_np_int) # 输出结果: [ 1 2 3 4 5 6 7 8 9 10]
# 设置元素全部是 5 的等差数列,默认为浮点数
ln_np = np.linspace(5, 5, 10)
print(ln_np) # 输出结果: [5. 5. 5. 5. 5. 5. 5. 5. 5. 5.]
# 设置 dtype为 int
ln_np = np.linspace(5, 5, 10, dtype = int)
print(ln_np) # 输出结果: [5 5 5 5 5 5 5 5 5 5]
# 默认包含 endpoint
df_np = np.linspace(10, 20, 5)
print(df_np) # 输出结果: [10. 12.5 15. 17.5 20. ]
# 设置 endpoint 为 false
df_np = np.linspace(10, 20, 5, endpoint = False, dtype = int)
print(df_np) # 输出结果: [10 12 14 16 18]
# # 设置 restep 为 True
rs_np = np.linspace(10, 20, 5, retstep = True)
print(rs_np) # 输出结果: (array([10. , 12.5, 15. , 17.5, 20. ]), 2.5)
numpy.logspace
创建等比数列
语法:
numpy.logspace(start, stop, num = 50, endpoint = True, base = 10.0, dtype = None)
| 参数 | 说明 |
|---|---|
| start | 序列的起始值,为 base**start |
| stop | 序列的终止值,为 base**stop.如果 endpoint为 True,则该值包含于数列中 |
| num | 要生成的等步长的样本数量,默认为 50 |
| endpoint | 该值为 True时,数列中包含 stop 值,反之不包含,默认是 True |
| base | 对数 log 的底数,参数表示取对数时 log 的下标 |
| dtype | ndarray 的数据类型 |
# 默认底数是 10
log_np = np.logspace(1.0, 2.0, num=10)
print(log_np)
# 输出结果:
# [ 10. 12.91549665 16.68100537 21.5443469 27.82559402
# 35.93813664 46.41588834 59.94842503 77.42636827 100. ]
# 对数设置为 2
ft_np = np.logspace(0, 9, 10, base = 2)
print(ft_np)
# 输出结果:
# [ 1. 2. 4. 8. 16. 32. 64. 128. 256. 512.]
# 设置数据类型为 int
int_np = np.logspace(0, 9, 10, base = 2, dtype = int)
print(int_np)
# 输出结果
# [ 1 2 4 8 16 32 64 128 256 512]
numpy.random.rand
返回[0,1]内的一组均匀分布的数
np.random.rand(d0, d1, d2,...dn)
print(np.random.rand(3))
# 输出结果:
# [0.21300967 0.96200165 0.263081 ]
print(np.random.rand(3,4))
# 输出结果:
# [[0.56638311 0.55940566 0.59616151 0.52539398]
# [0.76858897 0.64771629 0.95158406 0.05640405]
# [0.15460956 0.27146821 0.08352923 0.44875009]]
numpy.random.uniform
从一个均匀分布 [low,high)中随机采样
语法:
np.random.uniform(low = 0, high = 1.0, size = None)
print(np.random.uniform(3,5,4))
# 输出结果:
# [3.1709634 3.42339794 3.27909696 4.46827916]
print(np.random.uniform(3,5,(2,3)))
# 输出结果:
# [[3.9709236 3.93970353 4.31785822]
# [4.32227221 4.58652642 4.23842694]]
numpy.random.randint
从一个均匀分布中随机采样,生成一个整数或 n维整数数组
np.random.randint(low, high = None, size = None, dtype = np.int)
print(np.random.randint(2,5,4))
# 输出结果:
# [4 4 2 3]
print(np.random.randint(2,5,(2, 3)))
# 输出结果:
# [[2 4 3]
# [4 3 3]]
numpy.random.randn
从标准正态分布中返回一个或多个样本值
np.random.randn(d0, d1,...dn)
print(np.random.randn(3))
# 输出结果:
# [ 1.18209936 0.93089475 -2.47347065]
print(np.random.randn(2,3))
# 输出结果:
# [[-1.95040067 -0.8626327 0.17569308]
# [ 0.28590971 0.46468227 1.26280914]]
numpy.random.normal
返回指定格式的正态分布的数组
np.random.normal(loc = 0.0, scale = 1.0, size = None)
| 参数 | 说明 |
|---|---|
| loc | 概率分布的均值 |
| scale | 概率分布的标准差 |
| size | 整数或整数数组 |
print(np.random.normal(1,3,4))
# 输出结果:
# [-2.93258392 2.23256243 -3.12029932 4.10134292]
print(np.random.normal(1,3,(2,3)))
# 输出结果:
# [[ 1.56909099 -2.9939488 -0.82568105]
# [ 1.77511341 5.47539469 2.10879586]]
numpy.random.standard_normal
返回标准正态分布的数组
numpy.random.standard_normal(size = None)
print(numpy.random.standard_normal(4))
# 输出结果:
# [-0.01698748 0.81798313 -0.91465864 0.97863989]
print(numpy.random.standard_normal((2,3)))
# 输出结果:
# [[ 0.1126603 -0.43624923 0.71662159]
# [ 0.8407526 -0.49148368 -0.28130349]]